package org.tju.controller;

import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.tju.po.CommonResult;
import org.tju.po.User;
import org.tju.service.UserService;
import org.tju.util.TokenUtil;

//@CrossOrigin("*")
//跨域处理
@RestController
@RequestMapping("/UserController")
@RefreshScope
public class UserController {
	@Autowired
	private UserService userService;

	@GetMapping("/getUserByIdByPass/{userId}/{password}")
	public CommonResult<User> getUserByIdByPass(@PathVariable("userId") String userId,
                                                @PathVariable("password") String password) throws Exception {
		User param = new User();
		param.setUserId(userId);
		param.setPassword(password);
		User user = userService.getUserByIdByPass(param);
		return new CommonResult(200, "success", user);
	}

	@GetMapping("/getUserById/{tokenString}")
	public CommonResult<Integer> getUserById(@PathVariable("tokenString") String tokenString) throws Exception {
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		try{
			if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			int num = userService.getUserById(userId);
			return new CommonResult(200, "success", num);
			}
			else {
				return new CommonResult(200, "success", 0);
			}
		}
		catch (Exception e) {
			return new CommonResult(200, "success", 0);
		}
	}

	@PostMapping("/saveUser/{userId}/{password}/{userName}/{userSex}")
	public CommonResult<Integer> saveUser(@PathVariable("userId") String userId,
			@PathVariable("password") String password, @PathVariable("userName") String userName,
			@PathVariable("userSex") Integer userSex) throws Exception {
		User param = new User();
		param.setUserId(userId);
		param.setPassword(password);
		param.setUserName(userName);
		param.setUserSex(userSex);
		int result = userService.saveUser(param);
		return new CommonResult(200, "success", result);
	}

	//对用户信息进行更新操作
	@PutMapping("/updateUser/{tokenString}/{password}/{userName}/{userSex}")
	public CommonResult updateUser(@PathVariable("tokenString") String tokenString,
	@PathVariable("password") String password, @PathVariable("userName") String userName,
	@PathVariable("userSex") Integer userSex) throws Exception {

		User param = new User();
		param.setPassword(password);
		param.setUserName(userName);
		param.setUserSex(userSex);
		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return null;
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			param.setUserId(userId);
			User user2 =  userService.updateUser(param);
			user2.setToken(TokenUtil.tokenContent(tokenString));
			return new CommonResult(200, "success", user2);
		}else {
			return null;
		}
	}

	// 通过用户Id检查会员情况
	@PutMapping("/getMember/{tokenString}/{period}")
	public CommonResult getMember(@PathVariable("tokenString")String tokenString,
								  @PathVariable("period")Integer period) throws Exception{

		try {
			TokenUtil.verify(tokenString);
		}catch(Exception e){
			return new CommonResult(401, "Unauthorized", 0);
		}
		if(TokenUtil.verify(tokenString)) {
			String userId = TokenUtil.tokenUser(tokenString);
			User user = new User();
			user.setUserId(userId);
			user.setPeriod(period);
			User user2 = userService.getMember(user);
			user2.setToken(TokenUtil.tokenContent(tokenString));
			return new CommonResult(200, "success", user2);
		}else {
			return new CommonResult(401, "Unauthorized", 0);
		}
	}
}